﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CustomControls
{
    public partial class XButtonPanel : UserControl
    {
        public delegate void SelectedItemChangedHandler(object sender, int index);
        public event SelectedItemChangedHandler SelectedItemChanged;

        XButton.ButtonBehavior buttonBehavior;
        Font fontStyle;
        int selectedItem;

        public int SelectedItemIndex
        {
            get { return selectedItem; }
            set
            {
                selectedItem = value;
                OnSelectedItemChanged(value);
            }
        }

        private void OnSelectedItemChanged(int index)
        {
            if (SelectedItemChanged != null)
            {
                SelectedItemChanged(this, index);
            }
        }

        public bool SelectItem(int index)
        {
            try
            {
                if (SelectedItemIndex != 0)
                {
                    (this.Controls[index] as XButton).State = XButton.ButtonState.Normal;
                }
                (this.Controls[index] as XButton).State = XButton.ButtonState.Stucked;
                OnSelectedItemChanged(index);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void AddButton(XButton button)
        {
            button.Behavior = buttonBehavior;
            button.Font = fontStyle;
            button.Border = true;
            button.BorderColor = Color.Black;
            button.ForeColor = Color.LightGray;
            button.MouseDown += new MouseEventHandler(button_MouseDown);
            this.Controls.Add(button);
        }

        public XButtonPanel(XButton.ButtonBehavior behavior, Font font)
        {
            this.buttonBehavior = behavior;
            this.fontStyle = font;
        }

        void button_MouseDown(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < Controls.Count; i++)
            {
                if (!((XButton)(Controls[i])).Equals(sender)) ((XButton)Controls[i]).Unstuck();
                else if (SelectedItemIndex != i)
                {
                    SelectedItemIndex = i;
                }
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if ((double)this.ClientSize.Width / (double)this.ClientSize.Height > 1)
            {
                Size buttonSize = new Size((int)(this.ClientSize.Width / (double)Controls.Count), this.ClientSize.Height);
                for (int i = 0; i < Controls.Count; i++)
                {
                    Controls[i].Size = buttonSize;
                    Controls[i].Location = new Point(buttonSize.Width * i, 0);
                }
            }
            else
            {
                Size buttonSize = new Size(this.ClientSize.Width, (int)(this.ClientSize.Height / (double)Controls.Count));
                for (int i = 0; i < Controls.Count; i++)
                {
                    Controls[i].Size = buttonSize;
                    Controls[i].Location = new Point(0, buttonSize.Height * i);
                }
            }
        }
    }
}
